﻿
using MySql.Data.MySqlClient;
using newBlog.CenterInc.model;
using newBlog.inc.SqlINC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
 

namespace newBlog.CenterInc.Ctrltor
{
    public class fans_follow_inc
    {
        sqlConnect Conn = new sqlConnect();
        public int followCount(string user_id) {
            string sql = "select count(Blog_fans_follow) from blog_fans where Blog_user_id='{0}'";
            return Conn.DataValue(string.Format(sql, user_id));
        }//获取关注人数
        public List<Follow_fans_Dao> fllowMsg(string user_id,int page){
            List<Follow_fans_Dao> list = new List<Follow_fans_Dao>();
            string sql = "select " +
                "Blog_fans_follow," +
                "Blog_user_name," +
                "count(Blog_article_id) as articleCount," +
                "Blog_user_Grade," +
                "Blog_user_level ," +
                "(select count(Blog_user_id) from blog_fans where Blog_user_id=fans.Blog_fans_follow and Blog_fans_follow=fans.Blog_user_id) as isfollow " +
                "from " +
                "blog_fans as fans " +
                "LEFT JOIN blog_user as users on fans.Blog_fans_follow = users.Blog_user_id " +
                "LEFT JOIN blog_article as article ON fans.Blog_fans_follow = article.Blog_user_id " +
                "where fans.Blog_user_id = '{0}' " +
                "GROUP BY Blog_fans_follow " +
                "limit {1},20";
            MySqlDataReader dr = Conn.DataListAll(string.Format(sql,user_id,page));
            while(dr.Read()){
                Follow_fans_Dao follow_Fans_Dao = new Follow_fans_Dao(dr.GetString(0),dr.GetString(1),dr.GetString(2),dr.GetString(3),dr.GetString(4),dr.GetString(5));
                list.Add(follow_Fans_Dao);
            }
            return list;
        }//获取关注人数的信息

        public int FansCount(string user_id) {
            string sql = "select count(Blog_user_id) from blog_fans where Blog_fans_follow='{0}'";

            return Conn.DataValue(string.Format(sql,user_id));
        }
        public List<Follow_fans_Dao> fansMsg(string user_id,int page) {
            List<Follow_fans_Dao> list = new List<Follow_fans_Dao>();
            string sql = "SELECT " +
                "fans.Blog_user_id, " +
                "Blog_user_name, " +
                "count(Blog_article_id) AS articleCount, " +
                "Blog_user_Grade, " +
                "Blog_user_level, " +
                "(SELECT count(Blog_fans_follow)  FROM blog_fans  WHERE Blog_user_id = fans.Blog_fans_follow  AND Blog_fans_follow = fans.Blog_user_id  ) as isnotFollow " +
                "FROM " +
                "blog_fans AS fans " +
                "LEFT JOIN blog_user AS users ON fans.Blog_user_id = users.Blog_user_id " +
                "LEFT JOIN blog_article AS article ON fans.Blog_user_id = article.Blog_user_id " +
                "WHERE " +
                "fans.Blog_fans_follow = '{0}' " +
                "GROUP BY fans.Blog_user_id " +
                " limit {1},20";
            MySqlDataReader dr = Conn.DataListAll(string.Format(sql,user_id,page));
            while(dr.Read()){
                Follow_fans_Dao follow_Fans_Dao = new Follow_fans_Dao(dr.GetString(0),dr.GetString(1),dr.GetString(2),dr.GetString(3),dr.GetString(4),dr.GetString(5));
                list.Add(follow_Fans_Dao);
            }
            return list;

        }

        public bool SelectisFollow(string user_id,string follow_id) {
            string sql = "select count(*) from blog_fans where Blog_user_id='{0}' and Blog_fans_follow='{1}'";
            if (Conn.DataValue(string.Format(sql,user_id,follow_id))>0) {
                return true;
            }
            return false;
        }//检查是否关注
        public bool Follow(string user_id,string follow_id) {
            string sql = "insert into blog_fans(Blog_user_id,Blog_fans_follow) values('{0}','{1}')";
            if (Conn.AddDelUp(string.Format(sql,user_id,follow_id))<1) {
                return true;
            }
            return false;
        }//关注指定用户
        public bool delFollow(string user_id, string follow_id) {
            string sql = "delete from blog_fans where Blog_user_id='{0}' and Blog_fans_follow='{1}'";
            if (Conn.AddDelUp(string.Format(sql, user_id, follow_id)) < 1)
            {
                return true;
            }
            return false;
        }
    }

}